﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UMC.Data;
using UMC.WorkFlow.Entities;
using UMC.WorkFlow.Form;

namespace UMC.WorkFlow.Subassembly
{
    [UIDesign(Name = "子流程", Explain = "子流程结点")]
    public class TaskShape : UMC.WorkFlow.Shape, IDesignValueToText
    {
        [DesignAttribute(Caption = "启动流程", Explain = "激活后启动子流程", IsValueToText = true, ArgumentType = ArgumentType.Select, ArgumentValue = "Select")]
        public ulong? TaskId
        {
            get; set;
        }
        protected override void WriteProperty(TextWriter writer)
        {
            if (this.TaskId.HasValue)
            {
                writer.Write(',');
                JSON.Serialize("TaskId", this.TaskId, writer);
            }
            base.WriteProperty(writer);
        }
        public override void Read(string key, object value)
        {
            switch (key)
            {
                case "TaskId":
                    this.TaskId = Utility.UInt64Parse(value as string);
                    break;
                default:
                    base.Read(key, value);
                    break;
            }
        }
        public override void Activation(UMC.WorkFlow.Line line, UMC.WorkFlow.TaskInstance task, TaskRuntime runtime)
        {
            // task.
            if (this.Identity != null)
            {
                return;
            }
            if (this.TaskId.HasValue == false)
            {
                this.Identity = new Identity();
                return;
            }
            if (this.TaskId == task.TaskId)
            {
                throw new TaskMessageException("启动流程不能是当前流程");
            }
            var taskForm = DataFactory.Instance().TaskForm(TaskId.Value);

            if (taskForm == null)
            {
                this.Identity = new Identity();

            }
            var InstanceId = Utility.NewId();

            var ApplyNO =  SubassemblyHelper.ApplyNO(taskForm); 

            var noData = new TaskData
            {
                DataName = "ApplyNO",
                DataIndex = -1,
                DataText = ApplyNO,
                DataValue = ApplyNO,
                InstanceId = InstanceId
            };
            var pdata = new Hashtable();
            pdata["InstanceId"] = InstanceId;
            pdata["TaskId"] = TaskId;  
            pdata["ApplyNO"] = ApplyNO;

            pdata["Task.Text"] = taskForm.Caption;
            var subject = String.Format("{0}发起 {1} ", runtime.Engine.Identity.Alias, taskForm.Caption);
            if (String.IsNullOrEmpty(taskForm.SubjectFormat) == false)
            {
                subject = Utility.Format(taskForm.SubjectFormat, pdata);
            }

            DataFactory.Instance().Put(new TaskData
            {
                DataName = "Subject",
                DataIndex = -1,
                DataText = subject,
                DataValue = subject,
                InstanceId = InstanceId
            });



            UMC.WorkFlow.TaskRuntime.Submit(new Identity(runtime.Engine.Identity.Username, runtime.Engine.Identity.Alias), InstanceId, this.TaskId.Value, task.InstanceId, new FormMessage(new Hashtable()).OnRespond);

            this.Identity = new Identity(String.Format("~{0}", InstanceId), task.Text);
        }

        string IDesignValueToText.GetText(string name, object value)
        {
            switch (name)
            {
                case "TaskId":
                    var taskForm = DataFactory.Instance().TaskForm((ulong)value);

                    if (taskForm != null)
                    {
                        return String.Format("{0}({1})", taskForm.Caption, taskForm.Code);
                    }
                    break;

            }
            return value.ToString();

        }
    }
}
